package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.Permission;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.exception.UpdateException;
import com.zhitong.money.mapper.PermissionMapper;
import com.zhitong.money.mapper.RolePerMapper;
import com.zhitong.money.services.api.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PermissionService implements IPermissionService {
    @Autowired
    private PermissionMapper mapper;

    @Autowired
    private RolePerMapper rolePerMapper;


    @Override
    public List<Permission> findAllPermission() throws Exception{
            List<Permission> allPermission = mapper.findAllPermission();
            return allPermission;
    }

    @Override
    public List<Permission> findPersByRid(String rid) throws Exception {
        List<Permission> perByRid = mapper.findPermissionByRid(rid);
        return perByRid;
    }

    @Override
    public Permission findPermissionById(String id) throws Exception {
        Permission PermissionById = mapper.findPermissionById(id);
        if (PermissionById==null){
            throw new NullPointerException("findPermissionById:查询为空");
        }
        return PermissionById;
    }



    @Override
    public List<Permission> findPerByMap(Map<String, Object> map) throws Exception {
        List<Permission> PermissionByMap = mapper.findPermissionByMap(map);
        return PermissionByMap;
    }

    @Override
    public void addPermission(Permission Permission) throws Exception {
        try {
            Permission PermissionByName = mapper.findPermissionByName(Permission.getName());
            if (PermissionByName!=null){
                throw new RepeatException("信息重复!");
            }
            mapper.addPermission(Permission);
        } catch (Exception e) {
            throw new AddException("addPermission:添加失败!"+e.getMessage());
        }
    }

    @Override
    public PageInfo findAllByPage(Map<String, Object> map) throws Exception{
            int pageNum=map.containsKey("pageNum")?(int)map.get("pageNum"):1;
            int pageSize=map.containsKey("pageSize")?(int)map.get("pageSize"):5;
            PageHelper.startPage(pageNum,pageSize);
            List<Permission> PermissionByMap = mapper.findPermissionByMap(map);
            PageInfo p=new PageInfo(PermissionByMap);
            return p;
    }

    @Override
    public void updatePermission(Permission Permission) throws Exception{
        Permission PermissionByName = mapper.findPermissionByName(Permission.getName());
        if (PermissionByName!=null && !Permission.getId().equals(PermissionByName.getId())){
            throw new RepeatException("该资源已经存在!");
        }
        try {
            mapper.updatePermission(Permission);
        } catch (Exception e) {
            log.error("更新资源失败:"+e.getMessage());
            throw new UpdateException("updatePermission:更新失败");
        }
    }

    @Transactional
    @Override
    public int delPermission(String id) throws Exception{
        try {
            //check
            int i = rolePerMapper.countByPid(id);
            if (i==0){
                int i1 = mapper.delPermission(id);
                return i1;
            }
            return -1;
        } catch (Exception e) {
            log.error("删除资源失败!");
            throw new DeleteException("delPermission:删除失败");
        }
    }

    @Transactional
    @Override
    public int delPermissionByIds(List<String> ids) throws Exception{
        try {
            int i = rolePerMapper.countByPids(ids);
            if (i==0){
                int i1 = mapper.delPermissionByIds(ids);
                return i1;
            }
            return -1;
        } catch (Exception e) {
            log.error("删除资源失败!");
            throw new DeleteException("delPermission:删除失败");
        }
    }
}
